-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local class = require 'mc.class'
local singleton = require 'mc.singleton'
local service = require 'spdm.service'

local measurement_mdb = class()

function measurement_mdb:ctor(responder_collection)
    self.m_responder_collection = responder_collection
    self.m_mdb_measurements = {}
end

function measurement_mdb:init()
    self:regist_signals()
end

function measurement_mdb:regist_signals()
    self.m_responder_collection.m_measurement_added:on(function(...)
        self:new_measurement_to_mdb(...)
    end)
end

function measurement_mdb:new_measurement_to_mdb(integrity_id, info)
    local measurement_obj = service:CreateSPDMMeasurement(tostring(info.m_chassis_id), info.m_mdb_name,
        tostring(integrity_id), tostring(info.m_index), function(cur_obj)
        cur_obj.Index = info.m_index
        cur_obj.HashAlgorithm = info.m_hash_algo
        cur_obj.MeasurementType = info.m_type
        cur_obj.Measurement = info.m_measurement
        cur_obj.LastUpdated = info.m_last_updated
        cur_obj.PartofSummaryHash = info.m_part_of_summary_hash
        cur_obj.SecurityVersionNumber = info.m_security_version_number
    end)
    self.m_mdb_measurements[#self.m_mdb_measurements] = measurement_obj
end

return singleton(measurement_mdb)